﻿using LocalDbControler;
using RRQMSocket;
using RRQMSocket.RPC;
using System.Threading.Tasks;
using TransferModels;

namespace RemoteServer
{
    public static class Server
    {
        private static RPCClient client;
        private static RRQMRPC.RRQMWeChat.RPC_LoginServer loginServer;

        private static void ConnectServer()
        {
            if (client == null || !client.Connected)
            {
                client = new RPCClient();
                ConnectSetting connectSetting = new ConnectSetting();
                connectSetting.TargetIP = Controller.AppConfig.ServiceIP;
                connectSetting.TargetPort = Controller.AppConfig.RPCServicePort;
                connectSetting.MultithreadThreadCount = 1;
                client.Connect(connectSetting);
                client.InitializeRPC();
                client.SerializeConverter = new RRQMSocket_SerializeConverter.JsonTSerializeConverter();

                loginServer = new RRQMRPC.RRQMWeChat.RPC_LoginServer(client);
                MainLogger.WriteLog("RPC服务器连接成功");
            }
        }

        public async static void Test()
        {
            ConnectServer();
            string s =await loginServer.BeginTest("Test");
        } 
        
        public async static Task<RegistAccountResult> RegistAccount(string account,string password)
        {
            ConnectServer();
            
            return await loginServer.BeginRegistAccount(new RegistAccountRequest() {Account=account,Password=password });
        }
        
        public async static Task<ConfirmLoginResult> ConfirmLogin(string account,string password)
        {
            ConnectServer();
            
            return await loginServer.BeginConfirmLogin(new ConfirmLoginRequest() {Account=account,Password=password });
        }
    }
}